﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections;
using System.Collections.Generic;

namespace CamHelper.Interpreter
{
    public class ParameterCollection : IParameterCollection
    {
        private Dictionary<String, IParameter> _dictionary;

        public List<string> Categories { get; private set; }

        public int Count
        {
            get
            {
                return _dictionary.Count;
            }
        }

        public ParameterCollection()
        {
            _dictionary = new Dictionary<string, IParameter>();
            Categories = new List<string>();
        }

        public void Add(IParameter p)
        {
            if (!Categories.Contains(p.Category))
            {
                Categories.Add(p.Category);
            }

            if (_dictionary.ContainsKey(p.UniqueKey))
            {
                var old = _dictionary[p.UniqueKey];
                _dictionary.Remove(old.UniqueKey);
                p.BaseEntry = old;
            }
            _dictionary.Add(p.UniqueKey, p);
        }

        public void AddRange(IParameterCollection pc)
        {
            if (pc != null && pc.Count > 0)
            {
                foreach (var p in pc)
                {
                    Add(p.Value);
                }
            }
        }

        public bool Contains(string uniqueKey)
        {
            return _dictionary.ContainsKey(uniqueKey);
        }

        public IParameter this[string uniqueKey]
        {
            get
            {
                return _dictionary[uniqueKey];
            }
        }

        public IEnumerator<KeyValuePair<string, IParameter>> GetEnumerator()
        {
            return _dictionary.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}
